package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.system.NormalWeightItem;
import com.yanqu.road.entity.travel.TravelCity;
import com.yanqu.road.entity.travel.TravelEvent;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class TravelDaoImpl extends TempDao {

	public List<TravelCity> getTravelCityList() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_travel_city order by id";
		List<TravelCity> dataList = new ArrayList<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TravelCity data = new TravelCity();
				data.setCityId(rs.getInt("id"));
				data.setBeautyWeight(rs.getInt("specialEventPro"));
				List<NormalWeightItem> beautyEventList = getEventWeightList(rs.getString("cityEvents"));
				data.setBeautyEventWeightList(beautyEventList);
				List<NormalWeightItem> normalEventList = getEventWeightList(rs.getString("normalEvents"));
				data.setNormalEventWeightList(normalEventList);

				dataList.add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public Map<Integer, TravelEvent> getTravelEventMap() {
		Map<Integer, TravelEvent> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_travel_event";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TravelEvent data = new TravelEvent();
				data.setId(rs.getInt("id"));
				data.setEventType(rs.getInt("eventType"));
				//交易事件
				if(data.getEventType() == 12){
					data.setRewardStr(rs.getString("rewards"));
					data.setReward(new Property());
				}else {
					data.setReward(PropertyHelper.parseStringToProperty(rs.getString("rewards")));
				}
				data.setParamStr(rs.getString("additionalPar"));
				if(Objects.equals("0", data.getParamStr())){
					data.setNeedCalc(false);
				}else {
					data.setNeedCalc(true);
					String[] paramArr = data.getParamStr().split("\\|");
					data.setParam1(Integer.parseInt(paramArr[0]));
					data.setParam2(Integer.parseInt(paramArr[1]));
				}
				dataMap.put(data.getId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	private List<NormalWeightItem> getEventWeightList(String eventWeightParam){
		List<NormalWeightItem> eventList = new ArrayList<>();
		if(null == eventWeightParam){
			return eventList;
		}
		String[] eventParamArr = eventWeightParam.split("\\|");
		for(String eventParam : eventParamArr){
			String[] param = eventParam.split(",");
			NormalWeightItem weight = new NormalWeightItem();
			weight.setValue(new BigInteger(param[0]));
			weight.setWeight(Integer.parseInt(param[1]));
			eventList.add(weight);
		}
		return eventList;
	}
}
